﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.door
{
    public partial class door_index_img_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.sf_door_index_pics().Exists(this.id))
                {
                    JscriptMsg("门户活动图片不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("door_index_img_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }


        #region 赋值操作
        private void ShowInfo(int _id)
        {
            BLL.sf_door_index_pics bll = new BLL.sf_door_index_pics();
            Model.sf_door_index_pics model = bll.GetModel(_id); 
            if (!string.IsNullOrEmpty(model.classNum))
            {
                ddlDoorTypeId.SelectedValue = model.classNum;
            }
            if (model.isDelete == 0)
            {
                cbIsLock.Checked = true;
            }
            else
            {
                cbIsLock.Checked = false;
            }
            txtTitle.Text = model.name;
            txtContent.InnerText = model.remarks;
            txtPicUrl.Text = model.pics;
            imgHeadPic.ImageUrl = model.pics;
            txtImgUrl.Text = model.imgs;
            imgHeadImg.ImageUrl = model.imgs;
        }
        #endregion

        #region 增加操作
        private bool DoAdd()
        {
            try
            {
                Model.sf_door_index_pics model = new Model.sf_door_index_pics();
                BLL.sf_door_index_pics bll = new BLL.sf_door_index_pics();

                model.name = txtTitle.Text;
                model.remarks = txtContent.Value;
                model.pics = Request.Form["txtPicUrl"].Trim();
                model.imgs = Request.Form["txtImgUrl"].Trim();
                model.classNum = ddlDoorTypeId.SelectedValue.ToString();
                if (cbIsLock.Checked == true)
                {
                    model.isDelete = 0;
                }
                else
                {
                    model.isDelete = 1;
                }
                if (bll.Add(model) > 0)
                {
                    AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加门户活动图片：" + model.name); //记录日志
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.sf_door_index_pics bll = new BLL.sf_door_index_pics();
                Model.sf_door_index_pics model = bll.GetModel(_id);
                model.name = txtTitle.Text;
                model.remarks = txtContent.Value;
                model.pics = Request.Form["txtPicUrl"].Trim();
                model.imgs = Request.Form["txtImgUrl"].Trim();
                model.classNum = ddlDoorTypeId.SelectedValue.ToString();
                if (cbIsLock.Checked == true)
                {
                    model.isDelete = 0;
                }
                else
                {
                    model.isDelete = 1;
                }
                if (bll.Update(model))
                {
                    AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改门户活动图片：" + model.name); //记录日志
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存首页图片
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("door_index_img_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改门户活动图片成功！", "door_index_img_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("door_index_img_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加门户活动图片成功！", "door_index_img_list.aspx");
            }
        }

    }
}